<?php

namespace app\modules\Cruise\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\Cruise\models\Room;

/**
 * RoomSearch represents the model behind the search form about `app\modules\Cruise\models\Room`.
 */
class RoomSearch extends Room
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'windows', 'area', 'status', 'user_id', 'balcony', 'balcony_area', 'type', 'min'], 'integer'],
            [['cruise_id', 'name', 'code', 'num', 'ceil', 'desc', 'created_at', 'updated_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($cruise_id, $params)
    {
        $query = Room::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'cruise_id' => $cruise_id,
            'windows' => $this->windows,
            'area' => $this->area,
            'status' => $this->status,
            'type' => $this->type,
            'min' => $this->min,
            'user_id' => $this->user_id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'balcony' => $this->balcony,
            'balcony_area' => $this->balcony_area,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'num', $this->num])
            ->andFilterWhere(['like', 'ceil', $this->ceil])
            ->andFilterWhere(['like', 'desc', $this->desc]);

        return $dataProvider;
    }
}
